package com.example.webviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.webviewtest.R;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;


public class PingActivity extends AppCompatActivity {
    TextView txt;
    TextView ip;

    Button button;

    StringBuilder stringBuilder = new StringBuilder();
    Thread thread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ping);
        txt = findViewById(R.id.ping01);
        ip = findViewById(R.id.ip);
        button = findViewById(R.id.button);
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        if (ip.getText().toString().trim().equals("")) {
                            connectRequestUrl(ip.getHint().toString());
                        } else {
                            connectRequestUrl(ip.getText().toString());
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                thread.start();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public boolean connectRequestUrl (String ip) throws MalformedURLException {
        if (stringBuilder.length()>610) {
            stringBuilder = new StringBuilder();
        }
        long starttime = System.currentTimeMillis();
        HttpURLConnection connection = null;
        try {
            URL url = new URL("http://"+ip+":20022/download/test.jpg");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            // 设置超时时间，例如800ms
            connection.setConnectTimeout(800);
            connection.setReadTimeout(800);

            int responseCode = connection.getResponseCode();
            // 通常来说，200-299范围内的状态码表示成功的HTTP响应
            boolean b = responseCode >= 200 && responseCode < 300;
            if (b) {
                long endtime = System.currentTimeMillis();
                long l = endtime - starttime;
                stringBuilder.append(ip+"连接成功:"+l+"\n");
            } else {
                long endtime = System.currentTimeMillis();
                long l = endtime - starttime;
                stringBuilder.append(ip+"连接失败或超时:"+l+"\n");
            }
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    txt.setText(stringBuilder.toString());
                }
            });
            return b;
        } catch (IOException e) {
            // 捕获所有可能的IO异常，包括连接失败和读取失败
            e.printStackTrace();
            long endtime = System.currentTimeMillis();
            long l = endtime - starttime;
            stringBuilder.append(ip+"连接失败或超时:"+l+"\n");
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    txt.setText(stringBuilder.toString());
                }
            });
            return false;
        } finally {
            if (connection != null) {
                connection.disconnect(); // 断开连接以释放资源
            }
        }
    }
}